package com.qianfeng.wuyang.himalaya.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.activity.MainActivity;
import com.qianfeng.wuyang.himalaya.adapter.recommendadapter.RecommendDiscoveryAdapter;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.DiscoveryInfoEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.TopEntity;
import com.qianfeng.wuyang.himalaya.callback.RecommendFocusImgService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 *
 * 没用了已经
 *
 * 推荐界面discovery 发现新奇 viewpager 里的 fragment
 * A simple {@link Fragment} subclass.
 */
public class RecommendDiscoveryFragment extends Fragment implements Callback<TopEntity> {
    private RecyclerView recyclerView;
    private List<DiscoveryInfoEntity> list;
    private RecommendDiscoveryAdapter adapter;


    public static RecommendDiscoveryFragment newInstance() {
        
        Bundle args = new Bundle();
        
        RecommendDiscoveryFragment fragment = new RecommendDiscoveryFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View rootView = inflater.inflate(R.layout.fragment_recommend_pager,container,false);
        recyclerView= (RecyclerView) rootView.findViewById(R.id.recommend_pager_recycler);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(manager);

        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        list = new ArrayList<>();

        RecommendFocusImgService service = new Retrofit.Builder().baseUrl("http://mobile.ximalaya.com")
                .addConverterFactory(GsonConverterFactory.create()).build().create(RecommendFocusImgService.class);
        service.getFocusimgList("and-a1","android","true","true",2,"4.3.98").enqueue(this);


        adapter = new RecommendDiscoveryAdapter(list,getContext(), (MainActivity) getActivity());
        recyclerView.setAdapter(adapter);

    }

    @Override
    public void onResponse(Call<TopEntity> call, Response<TopEntity> response) {

        if (response.body().getDiscoveryColumns().getList()!=null) {
            list.addAll(response.body().getDiscoveryColumns().getList());
        }
        adapter.addDatas(list);

    }

    @Override
    public void onFailure(Call<TopEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
    }
}
